<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>5+2商城</title>
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="css/detail.css"/>
    <script src="js/jquery.min.js"></script>
    <script src="js/Underscore-min.js"></script>
    <script src="js/MyFunc.js"></script>
    <script src="js/tishikuang.js"></script>
    <script src="js/jquery.cookie.js"></script>
    <link rel="stylesheet" href="css/orderstyle.css">


</head>
<body>
<div >

    <!--头部-->
    <header id="header" class="headers">
        <div id="header_in" class="headers-in">
            <div class="headers-left">
                <ul>
                    <li class="logo-pic" style="margin-top:15px"><h1><a href="index.html">5+2商城</a></h1></li>
                    <li><a href="">发现</a></li>
                    <li><a href="">最新</a></li>
                    <li><a href="">美思</a></li>
                    <li><a href="">活动<span class="new">new</span></a></li>
                    <li><a href="">教育</a></li>
                    <li><a href="">...</a></li>
                </ul>
            </div>
            <div class="headers-center">
                <input id="query" name="query" size="27" type="text" placeholder="搜索你喜欢的" value="">
            </div>
        </div>
    </header>
    <!--快速登录-->
    <!--吸顶效果-->
    <section id="top_nav" class="top-nav">
        <div class="top-nav-in">
            <div class="left">
                <p>国内最优质图片灵感库</p>
                <p>已有数百万出众网友，用花瓣保存喜欢的图片。</p>
            </div>
            <div class="right">
                <ul>
                    <li><p>用社交帐号直接登录：</p></li>
                    <li><a href=""></a></li>
                    <li><a href=""></a></li>
                    <li><a href=""></a></li>
                    <li><a href=""></a></li>
                </ul>
            </div>
        </div>
    </section>
    <!--类型标题-->
    <!--选项卡-->
    <div id="tab" class="tab">
        <div id="tab_header" class="tab-header">
        </div>
        <div id="tab_content" class="tab-content">
            <div class="dom" style="display: block">
                <div id="page">
                    <div id="content">

                        <div class="tm-chaoshi-nav" data-spm="1998159256">
                            <div class="layout">
                                <div class="nav-extra">
                                    <div id="cat">商品信息修改</div>
                                </div>
                            </div>
                        </div>
                        <div class="tm-crumbs">
                            <div class="tm-content">
                                <ul class="nav J_MallCrumbs"></ul>
                            </div>
                        </div>

                        <div id="detail">
                            <div id="J_DetailMeta" class="tm-detail-meta tm-clear">
                                <div class="tm-clear">
                                    <div class="tb-property" style="margin-top: 80px">
                                        <div class="tb-wrap" id="goods_detail">
                                            <ul class="list-head" id="info_box">

                                            </ul>

                                        </div>
                                    </div>
                                    <div class="tb-gallery">
                                        <div class="tb-booth">
                                            <a href=""  id="picbox">

                                            </a>
                                        </div>

                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>
</div>

<!--右边浮动-->
<div id="elevator_item" class="elevator-item">
    <a onclick="window.scrollTo(0,0);" class="plus"><img src="images/返回顶部.png" alt=""></a>
    <br>
</div>
<!--登录面板-->

<script>

    // var BasicUrl="http://192.168.43.205:8080/52在线商城/";
    var BasicUrl=jQuery.getCookie("BasicUrl");
    var str=location.href; //取得整个地址栏
    var id_str=str.split("=");//分割参数
    var user_id=jQuery.getCookie("userId");
    var seller_id=jQuery.getCookie("sellerId");
    var store_id=0;
    var isLogin=false; //判断登陆与否
    // console.log(num[1]);


    window.onload=function () {
        if(jQuery.getCookie("userId")!==undefined){
            isLogin=true;
        }
        jQuery.ajax({
            //添加请求头以验证用户身份

            type: 'POST',
            url: BasicUrl+ "proById",
            // data:{P_id:id_str[1]},
            data:{P_id:id_str[1]},

            dataType: 'json',
            success: function (datas) {
                // var json_data = JSON.stringify(datas);
                console.log( datas);
                var goods_details2=
                    '<li class="list-group-item">'
                    +'<span class="title">商品名</span>'
                    +'<input type="text" id="p_name" class="select_input" value="'+datas.P_name+'">'
                    +'</li>'
                    +'<li class="list-group-item">'
                    +'<span class="title">描述</span>'
                    +'<textarea  id="p_desc" class="select_input" style="height:6rem">'+datas.P_desc+'</textarea>'
                    +'</li>'
                    +'<li class="list-group-item">'
                    +'<span class="title">价格</span>'
                    +'<input type="text" id="p_price" class="select_input" value="'+datas.P_price+'">'
                    +'</li>'
                    +'<li class="list-group-item">'
                    +'<span class="title">库存量</span>'
                    +'<input type="text" id="p_inventory" class="select_input" value="'+datas.P_inventory+'">'
                    +'</li>'
                    +'<div class="tb-action tm-clear">'
                    +' <div class="tb-btn-buy tb-btn-sku">'
                    // +'<a href="">立即购买</a>'
                    +'</div>'
                    +'<div class="tb-btn-basket tb-btn-sku tm-chaoshi-add" >'
                    +'<a onclick="dele()" style="cursor: pointer;margin-right: 3rem">删除商品</a>'
                    +'<a onclick="update()" style="cursor: pointer">提交修改</a>'
                    +'</div>'
                    +'</div>';

                var picture='<img src="'+datas.P_image+'" alt="">';
                // jQuery('#goods_detail').append(goods_detail);
                jQuery('#info_box').append(goods_details2);
                jQuery('#picbox').append(picture);
                store_id=datas.St_no;


            },
            error: function (xhr, type) {
                tishi('请求失败');
            }
        });

    };
    //提交修改按钮
    function update() {
        console.log(typeof seller_id);
        console.log(typeof store_id);

        if(seller_id!=store_id){
            tishi("这不是你的商品，无权操作")
        }else {
            var key_arr = ["p_id"];
            var value_arr = [id_str[1]];
            // var register_mask=jQuery('#regiser_mask');
            var inputArr = jQuery('input');//获取input控件
            var textareaArr = jQuery('textarea');//获取input控件
            var selectArr = jQuery('select');//获取select控件
            //3.循环处理input,并定义结果集
            selectArr.each(function () {
                //4.将每个input的值放进结果集
                value_arr.push(jQuery(this).val());
                key_arr.push(jQuery(this).attr('id'));
            });
            inputArr.each(function () {
                //4.将每个input的值放进结果集
                value_arr.push(jQuery(this).val());
                key_arr.push(jQuery(this).attr('id'));
            });
            textareaArr.each(function () {
                //4.将每个input的值放进结果集
                value_arr.push(jQuery(this).val());
                key_arr.push(jQuery(this).attr('id'));
            });
            var update_data = {};
            addJson(update_data, key_arr, value_arr);//组织ajax 请求所需data
            jQuery.ajax({
                //添加请求头以验证用户身份
                // beforeSend: function(XHR) {
                //     XHR.setRequestHeader("accesstoken", usertoken);
                // },
                type: 'POST',
                data: update_data,
                url: BasicUrl + "ServletProUpdate",
                dataType: 'json',
                success: function (datas) {
                    console.log(datas);
                    console.log(datas.state);
                    if (datas.state === true) {
                        tishi("修改成功");
                        setTimeout('location.reload()', 1000)
                    } else {
                        tishi("修改失败，请重试");
                    }


                },
                error: function (xhr, type) {
                    tishi('请求失败');
                    // 即使加载出错，也得重置
                }
            });
        }
    }
    function dele() {
        if(seller_id!=store_id){
            tishi("这不是你的商品，无权操作")
        }else{
            jQuery.ajax({
                //添加请求头以验证用户身份
                // beforeSend: function(XHR) {
                //     XHR.setRequestHeader("accesstoken", usertoken);
                // },
                type: 'POST',
                data:{p_id:id_str[1]},
                url: BasicUrl+"ServletProDel",
                dataType: 'json',
                success: function(datas) {
                    console.log(datas.state);
                    if(datas.state===true){
                        tishi("修改成功");
                        setTimeout('location.reload()',1000)
                    }else{
                        tishi("修改失败，请重试");
                    }
                },
                error: function(xhr, type){
                    tishi('请求失败');
                    // 即使加载出错，也得重置
                }
            });
        }

    }

    function $$(id) {
        return typeof id === "string" ? document.getElementById(id) : null;
    }

    //组织json
    function addJson(data, key_arr, var_arr) {
        for (var i = 0; i < key_arr.length; i++) {
            data[key_arr[i]] = var_arr[i];
            // if (var_arr[i] !== "") {
            //     data[key_arr[i]] = var_arr[i];
            // } else {
            //     delete data[key_arr[i]];
            // }
        }
        data = JSON.stringify(data);
        console.log("data为:" + data);

    }

</script>
</body>
</html>